package com.finchina.myview.viewgroups;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.finchina.myview.R;

/**
 * <b>Project:</b> MyView<br>
 * <b>Create Date:</b> 2016/10/19 0019<br>
 * <b>Author:</b> huang saisai <br>
 * <b>Description:</b> <br>
 */
public class PercentLayout extends RelativeLayout {
    public PercentLayout(Context context) {
        super(context);
    }

    public PercentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PercentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width= MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        float widthPercent = 0, heightPercent = 0;

        for (int i=0, size = getChildCount(); i <size; i++){
            View view = getChildAt(i);
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams instanceof PercentLayoutParams){
                widthPercent = ((PercentLayoutParams) layoutParams).getWidthPercent();
                heightPercent = ((PercentLayoutParams) layoutParams).getHeightPercent();
            }
            if (widthPercent != 0){
                layoutParams.width = (int) (widthPercent * width);
            }
            if (heightPercent != 0){
                layoutParams.height = (int) (heightPercent * height);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new PercentLayoutParams(getContext(),attrs);
    }

    public static class PercentLayoutParams extends RelativeLayout.LayoutParams{
        float widthPercent, heightPercent;

        public float getWidthPercent() {
            return widthPercent;
        }

        public void setWidthPercent(float widthPercent) {
            this.widthPercent = widthPercent;
        }

        public float getHeightPercent() {
            return heightPercent;
        }

        public void setHeightPercent(float heightPercent) {
            this.heightPercent = heightPercent;
        }

        public PercentLayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray ta = c.obtainStyledAttributes(attrs, R.styleable.PercentLayout);
            widthPercent = ta.getFloat(R.styleable.PercentLayout_layout_widthpercent, 1);
            heightPercent = ta.getFloat(R.styleable.PercentLayout_layout_heightpercent, 1);

            ta.recycle();
        }
    }
}
